//
//  GenerateQRCodeViewController.m
//  BarCodeAndQRCode
//
//  Created by jiezsoft.com on 14-10-10.
//  Copyright (c) 2014年 jiezsoft.com. All rights reserved.

#import "GenerateQRCodeViewController.h"
#import "QRCodeGenerator.h"
#import "addhuiyuanViewController.h"
@interface GenerateQRCodeViewController ()<UIScrollViewDelegate,UIAlertViewDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate>

@end

@implementation GenerateQRCodeViewController
{
   UIScrollView*mainScroll;
    HAODEsingleton* commdity;
    UILabel *labelzhanghao;
    UILabel *labelTel;
    UILabel *labelsfz;
    UILabel *labelAddress1;
    UILabel *labelAddress ;
    UILabel*labelstore;
    UILabel *labelzhanghao1;
    UILabel *labelTel1;
    UILabel *labelsfz1;
    UILabel *labelAddress11;
    UIButton* btn;
    UILabel*labelstore1;
    UIButton *time;
    NSString *sn;
    
 
}
-(void)BackAction:(UIButton *)sender{
      [time removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
   
    
    if ([self.QrCode isEqualToString:@"GetReadMemberQrCode"]) {
        
        UIButton*sxBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0, ScreenWidth*65/750, 30)];
        [sxBtn setTitle:@"取消" forState:UIControlStateNormal];
        
        [sxBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        sxBtn.titleLabel.font=[UIFont systemFontOfSize:contenfot];
        [sxBtn addTarget:self action:@selector(quxiao:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:sxBtn];
        self.navigationItem.rightBarButtonItem = myBtnBtn;

        self.title = @"二维码";
    
    }else{
    
    
    self.title = @"扫公众号二维码";
    
      
      
    }

    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];

    commdity=[HAODEsingleton shared];

    
    
    
    mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight)];
   
    [self.view addSubview:mainScroll];

    
    mainScroll.delegate = self;
    mainScroll.userInteractionEnabled = YES;
    mainScroll.scrollEnabled = YES;
    mainScroll.showsVerticalScrollIndicator = FALSE;
    mainScroll.showsHorizontalScrollIndicator = FALSE;

    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSString* companyId = [user objectForKey:@"companyId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    
    if ([self.QrCode isEqualToString:@"GetReadMemberQrCode"]) {
      
        
        
    }else{
         [params setValue:companyId forKey:@"companyID"];
    }
    [params setValue:Token forKey:@"tokenId"];
    
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    
    
    [HttpRequest requestData:[NSString stringWithFormat:@"Company/%@" ,self.QrCode]  params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                NSLog(@"%@",severDataDict.dataDict);
               
               
                
                
                sn=[severDataDict.dataDict  objectForKey:@"sn"];
                
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                [user setObject:severDataDict.dataDict forKey:@"qr"];
                [user synchronize];
//
//
//                               
//                NSString * str=[NSString stringWithFormat:@"%.f",  385/2];
//                NSString * str1=[NSString stringWithFormat:@"%.f",   ScreenHeight*390/1334];
               
                
                UIButton* QRCode1=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth*85/750,ScreenHeight*370/1334, 385/2,385/2) ];
                QRCode1.center=mainScroll.center;
                [mainScroll addSubview:QRCode1];
                
               
                
                if ([self.QrCode isEqualToString:@"GetReadMemberQrCode"]) {
                    
                    [self times];
                    [QRCode1 setImageWithURL:[NSURL URLWithString:[severDataDict.dataDict  objectForKey:@"qrcodeUrl"]]];
                    UIButton* logo=[[UIButton alloc]initWithFrame:CGRectMake(0,0, 30,30) ];
                    [logo setImageWithURL:[NSURL URLWithString:[severDataDict.dataDict  objectForKey:@"logoUrl"]]];
                    logo.center=QRCode1.center;
                    [mainScroll addSubview:logo];
                }else{
                    UIButton* logo=[[UIButton alloc]initWithFrame:CGRectMake(0,0, 30,30) ];
                    [logo setImageWithURL:[NSURL URLWithString:[severDataDict.dataDict  objectForKey:@"logoUrl"]]];
                    logo.center=QRCode1.center;
                    [mainScroll addSubview:logo];
                    [QRCode1 setImageWithURL:[NSURL URLWithString:[severDataDict.dataDict  objectForKey:@"qrCodeUrl"]]];
                }
                
               
                UILabel* tishi=[[UILabel alloc]initWithFrame:CGRectMake(0, ScreenHeight/2-144,ScreenWidth,12)];
                tishi.text=@"对准二维码，即可自动扫描";
                tishi.textColor=zhonghuiColor;
                tishi.textAlignment = NSTextAlignmentCenter;
                
                tishi.font=[UIFont systemFontOfSize:12];
                [mainScroll addSubview:tishi];
              
                
                

                UIButton* biankuan=[[UIButton alloc]initWithFrame:CGRectMake(0,0, 550/2,550/2) ];
                [biankuan setBackgroundImage:[UIImage imageNamed:@"qujingkuang"] forState:UIControlStateNormal];
                biankuan.center=QRCode1.center;
                [mainScroll addSubview:biankuan];

                [mainScroll setContentSize:CGSizeMake(ScreenWidth-20, QRCode1.bottom+50)];
       
            
        
    }else{
        [Global hideProgressHUDInternal:self.view];
        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
        [Global showTextHudSub:requestResultStr view:self.view];
    }
     
     }
     else
     {
         [Global hideProgressHUDInternal:self.view];
         NSLog(@"aaaaaaaa%@",severDataDict.dataDict);
         [Global showTextHudSub:@"连接失败" view:self.view];
     }
     
     }];
    

    
}

- (void)times{

    time = [[UIButton alloc]init];
    time.frame = CGRectMake(ScreenWidth-100,ScreenHeight*35/2200,30,16);
    [time setTitleColor:titleColor forState:UIControlStateNormal];
    [time setTitle:[NSString stringWithFormat:@"%d",10] forState:UIControlStateNormal];
    time.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [mainScroll addSubview:time];
    
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownActions:) userInfo:time repeats:YES];
    
    
}
//自动更新
-(void)countDownActions:(NSTimer*)timer{
    
    time = timer.userInfo;
    
    if (!time.superview) {
        [timer invalidate];
        return;
    }
    int currentTime = [[time titleForState:UIControlStateNormal] intValue];
    [time setTitle:[NSString stringWithFormat:@"%d",--currentTime] forState:UIControlStateNormal];
    time.titleLabel.font = [UIFont systemFontOfSize:16];
    if (currentTime<1) {
         [time setTitle:[NSString stringWithFormat:@"%d",5] forState:UIControlStateNormal];
        [self times];
        [time removeFromSuperview];
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* Token = [user objectForKey:@"tokenId"];
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:Token forKey:@"tokenId"];
        [params setValue:sn forKey:@"sn"];

        [HttpRequest requestData:@"Company/GetMemberByQrCode" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                NSLog(@"%@",severDataDict.dataDict);
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
 
                    commdity.ImageUrl=[severDataDict.dataDict objectForKey:@"headImgUrl"];
                    commdity.NickName=[severDataDict.dataDict objectForKey:@"nickName"];
                    commdity.Sex=[NSString stringWithFormat:@"%@",  [severDataDict.dataDict objectForKey:@"sex"]];
                    commdity.OpenID=[severDataDict.dataDict  objectForKey:@"openID"];
                    [time setTitle:[NSString stringWithFormat:@"%d",10000000] forState:UIControlStateNormal];
                    [self.navigationController popViewControllerAnimated:YES];
                    [time removeFromSuperview];
                    [timer invalidate];
                    
            
            
        }else{
        }
         
         }
         else
         {
             [Global hideProgressHUDInternal:self.view];
             NSLog(@"aaaaaaaa%@",severDataDict.dataDict);
             [timer invalidate];
             [Global showTextHudSub:@"连接失败" view:self.view];
         }
         
         }];
      
        
        
        
        
        
        
    }
}

-(void)quxiao:(UIButton *)sender{
  [time removeFromSuperview];
 [self.navigationController popViewControllerAnimated:YES];


}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 0) {
        
        
//        
//        TheLoginViewController*vc1=[[TheLoginViewController alloc] init];
//        
//        [self.navigationController pushViewController:vc1 animated:YES];
//        
        
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}



@end
